/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managerBeans;

import entity.Job;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.context.RequestContext;
import sessionBeans.JobFacadeLocal;

/**
 *
 * @author Minh Luc
 */
@ManagedBean
@SessionScoped
public class JobManagement implements Serializable {

    @EJB
    private JobFacadeLocal jobFacade;
    private Job job = new Job();
    private Job selectJob = new Job();
    private List<Job> list = null;
    private List<Job> filteredList = null;

    public void create() {
        if (!validate(job)) {
            return;
        }

        RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg = null;
        try {
            jobFacade.create(job);
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Create Job", "Create Successful");
        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", e.getMessage() + " Duplicate Name");
        }
        job = new Job();
        FacesContext.getCurrentInstance().addMessage(null, msg);
        context.addCallbackParam("create", true);
    }

    public void edit() {
        if (!validate(selectJob)) {
            return;
        }

        RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg = null;

        try {
            jobFacade.edit(selectJob);
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Edit Job", "Edit Successful");
        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", e.getMessage());
        }

        FacesContext.getCurrentInstance().addMessage(null, msg);
        context.addCallbackParam("edit", true);
    }

    public Job getJob() {
        return job;
    }

    public void setJob(Job job) {
        this.job = job;
    }

    public Job getSelectJob() {
        return selectJob;
    }

    public void setSelectJob(Job selectJob) {
        this.selectJob = selectJob;
    }

    public List<Job> getList() {
        list = jobFacade.findAll();
        return list;
    }

    public void setList(List<Job> list) {
        this.list = list;
    }

    public List<Job> getFilteredList() {
        return filteredList;
    }

    public void setFilteredList(List<Job> filteredList) {
        this.filteredList = filteredList;
    }

	//Validate Job
    public boolean validate(Job job) {
        RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg = null;

        if (job.getJobTitle() == null || job.getJobTitle().length() == 0) {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "JobTitle is not null");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return false;
        } else {
            if (job.getJobTitle().length() < 2 || job.getJobTitle().length() > 30) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "JobTitle is from 2 - 30 Characters");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
            if (!util.Validate.isContent(job.getJobTitle())) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "JobTitle is only character");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
        }

        if (job.getJobDescripton() == null || job.getJobDescripton().length() == 0) {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "JobDescription is not null");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return false;
        } else {
            if (job.getJobDescripton().length() < 2 || job.getJobDescripton().length() > 300) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "JobDescription is from 2 - 300 Characters");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
            if (!util.Validate.isContent(job.getJobDescripton())) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "JobDescription is only Character");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
        }

        if (job.getJobLimitPerYear() == null || job.getJobLimitPerYear().toString().length() == 0) {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "JobLimitPerYear is not null");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return false;
        } else {
            if (job.getJobLimitPerYear().toString().length() > 15) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "JobLimitPerYear is less than 10 Numbers");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
            if (!util.Validate.isFloat(job.getJobLimitPerYear().toString())) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "JobLimitPerYear is only Number");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
        }
        return true;
    }
}
